//
//  HYBDPanCollectionView.m
//  XPlayer
//
//  Created by 张水 on 2023/9/21.
//

#import "HYBDPanCollectionView.h"
#import "HYBDPanFileModel.h"
#import "HYBDPanCollectionCell.h"

@interface HYBDPanCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HYBDPanCollectionView

- (instancetype)initWithViewModel:(HYBDPanFileListViewModel *)viewModel fileType:(BDPanFileType)bdFiletype
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        self.bdFiletype = bdFiletype;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}


- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.viewModel numberOfSectionsInCollectionView:section withFileType:self.bdFiletype];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYBDPanCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CEllIDHYBDPanCollectionCell" forIndexPath:indexPath];
    HYBDPanFileModelList *listData = [self.viewModel cellForRowAtIndexPathInCollectionView:indexPath withFileType:self.bdFiletype];
    [cell bindViewModel:listData];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectItemAtIndexPath:indexPath withFileType:self.bdFiletype];
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        NSInteger cloum = kIsIPad?3:2;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(cloum), kItemHeight(cloum));
        layout.minimumLineSpacing = kItemSpace;
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#FFFFFF" darkModeHex:@"#181A22"];
        _collectionView.contentInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, kItemSpace, kItemSpace);
        [_collectionView registerClass:[HYBDPanCollectionCell class] forCellWithReuseIdentifier:@"CEllIDHYBDPanCollectionCell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

@end
